package com.yongbao.common.config;

import com.wechat.pay.contrib.apache.httpclient.WechatPayHttpClientBuilder;
import com.wechat.pay.contrib.apache.httpclient.auth.AutoUpdateCertificatesVerifier;
import com.wechat.pay.contrib.apache.httpclient.auth.PrivateKeySigner;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Credentials;
import com.wechat.pay.contrib.apache.httpclient.auth.WechatPay2Validator;
import com.wechat.pay.contrib.apache.httpclient.util.PemUtil;
import org.apache.http.impl.client.CloseableHttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.security.PrivateKey;

/**
 * @author zhanghong
 * @version ConfigurationConfig.java, v 0.1 2022/1/7 15:05 下午 ConfigurationConfig Exp $$
 */
@Component
public class ConfigurationConfig {

    public static String applicationName;

    /**
     * 用户端小程序支付商户证书 apiclient_key.pem
     */
    @Value("${wx.user.wechat.pay_api_client_key}")
    public String wxUserWechatPayApiClientKey;
    /**
     * 用户端小程序商户id
     */
    @Value("${wx.user.wechat.merchant_id}")
    public String wxUserWechatMerchantId;
    /**
     * 用户端小程序证书序列号
     */
    @Value("${wx.user.wechat.merchant_serial_no}")
    public String wxUserWechatMerchantSerialNo;
    /**
     * 用户端小程序支付 API v3 秘钥
     */
    @Value("${wx.user.wechat.apiV3Key}")
    public String wxUserWechatApiV3Key;

    @Value("${spring.application.name}")
    public void setApplicationName(String applicationName) {
        ConfigurationConfig.applicationName = applicationName;
    }

//    @Bean
//    public CloseableHttpClient httpClient() throws IOException {
//        // 加载商户私钥（privateKey：私钥字符串）
//        PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(new ByteArrayInputStream(wxUserWechatPayApiClientKey.getBytes("utf-8")));
//
//        // 加载平台证书（mchId：商户号,mchSerialNo：商户证书序列号,apiV3Key：V3密钥）
//        AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(new WechatPay2Credentials(wxUserWechatMerchantId, new PrivateKeySigner(wxUserWechatMerchantSerialNo, merchantPrivateKey)), wxUserWechatApiV3Key.getBytes("utf-8"));
//
//        // 初始化httpClient
//        CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create()
//                .withMerchant(wxUserWechatMerchantId, wxUserWechatMerchantSerialNo, merchantPrivateKey)
//                .withValidator(new WechatPay2Validator(verifier)).build();
//        return httpClient;
//    }
}
